using System;
using System.Collections.Generic;
using System.Windows;
using DotNet.Utilities;
using WPFPractice.Utils;
using WPFPractice.ViewModels;
using WPFTemplateLib.Enhance;
using WPFTemplateLib.WpfHelpers;

namespace WPFPractice.Windows
{
	public partial class WinEnhanceClassTest : Window
	{
		public WinEnhanceClassTest()
		{
			InitializeComponent();
			DataContext = new WinEnhanceClassTestViewModel();
		}
	}
}

namespace WPFPractice.ViewModels
{
	public class WinEnhanceClassTestViewModel : MyViewModelBase
	{
		#region 成员、构造

		public WinEnhanceClassTestViewModel()
		{
			InitData();
		}

		#endregion

		#region 绑定

		/// <summary>
		/// 具有变动通知功能的字典
		/// </summary>
		public ObservableDictionary<string, int> ObservableDict { get; set; } = new();

		/// <summary>
		/// 选中的字典项
		/// </summary>
		public KeyValuePair<string, int>? SelectedDictItem { get; set; }

		#endregion

		#region 命令

		#region [命令] 重载数据
		private RelayCommand _ReloadCmd;
		public RelayCommand ReloadCmd => _ReloadCmd ??= new RelayCommand(ExecuteReloadCmd);
		private void ExecuteReloadCmd()
		{
			try
			{
				InitData();
				ToastToScreen("重载数据成功");
			}
			catch(Exception ex)
			{
				Console.WriteLine(ex);
			}
		}
		#endregion

		#region [命令] 改变选中项的 Value 值
		private RelayCommand _ChangeSelectedItemValueCmd;
		public RelayCommand ChangeSelectedItemValueCmd => _ChangeSelectedItemValueCmd ??= new RelayCommand(ExecuteChangeSelectedItemValueCmd);
		private void ExecuteChangeSelectedItemValueCmd()
		{
			try
			{
				if(SelectedDictItem == null)
					return;

				var selectedItem = SelectedDictItem.Value;
				if(ObservableDict.ContainsKey(selectedItem.Key))
				{
					// 修改选中项的 Value 值
					ObservableDict[selectedItem.Key] = selectedItem.Value + 1;
					ToastToScreen($"已将选中项 {selectedItem.Key} 的值改为 {ObservableDict[selectedItem.Key]}");
				}
				else
				{
					ToastToScreen("选中项不存在于字典中");
				}
			}
			catch(Exception ex)
			{
				Console.WriteLine(ex);
			}
		}
		#endregion

		private RelayCommand _ChangeSelectedItemKeyCmd;
		public RelayCommand ChangeSelectedItemKeyCmd => _ChangeSelectedItemKeyCmd ??= new RelayCommand(ExecuteChangeSelectedItemKeyCmd);
		private void ExecuteChangeSelectedItemKeyCmd()
		{
			try
			{
				if(SelectedDictItem == null)
					return;

				var selectedItem = SelectedDictItem.Value;
				if(ObservableDict.ContainsKey(selectedItem.Key))
				{
					// 修改选中项的 Key 值
					var newKey = $"{selectedItem.Key}_Modified";
					ObservableDict[newKey] = ObservableDict[selectedItem.Key];
					ObservableDict.Remove(selectedItem.Key);
					ToastToScreen($"已将选中项 {selectedItem.Key} 的 Key 改为 {newKey}");
				}
				else
				{
					ToastToScreen("选中项不存在于字典中");
				}
			}
			catch(Exception ex)
			{
				Console.WriteLine(ex);
			}
		}

		#endregion

		#region 方法

		/// <summary>
		/// 初始化数据
		/// </summary>
		private void InitData()
		{
			ObservableDict = new ObservableDictionary<string, int>();
			var weekList = EnumExtension.GetEnumList<DayOfWeek>();
			foreach(var week in weekList)
			{
				ObservableDict.Add(week.ToString(), (int)week);
			}

			//var dict = ClassHelper.CloneObject(ObservableDict);
		}

		#endregion
	}
}
